package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/testdata/protoexample"
	"log"
	"net/http"
	"time"
)

func main() {
	q := gin.Default()
	q.GET("/someJson", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{"message":"someJSON","status":http.StatusOK})
	})
	q.GET("/someStruct", func(c *gin.Context) {
		var msg struct{
			Name    string
			Message string
			Number  int
		}
		msg.Name = "root"
		msg.Message = "message"
		msg.Number = 123
		c.JSON(http.StatusOK,msg)
	})
	//XML
	q.GET("/someXML", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{"message": "abc"})
	})
	//YML
	q.GET("/someYML", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{"name": "zhangsan"})
	})
	// 5.protobuf格式,谷歌开发的高效存储读取的工具
	// 数组？切片？如果自己构建一个传输格式，应该是什么格式？
	q.GET("/someProtoBuf", func(c *gin.Context) {
		resp:= []int64{int64(1),int64(2)}
		// 定义数据
		label := "label"
		data := &protoexample.Test{
			Label:            &label,
			Reps:             resp,
		}
		c.ProtoBuf(http.StatusOK,data)
	})

	//since
	q.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK,c.QueryArray("media"))
	})

	//map
	q.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK,c.QueryMap("media"))
	})

	//重定向
	q.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently,"http://www.5lmh.com")
	})

	//异步
	q.GET("/", func(c *gin.Context) {
		copyContext := c.Copy()
		//异步
		go func() {
			time.Sleep(3*time.Second)
			log.Println("异步执行：" + copyContext.Request.URL.Path)

		}()
	})
	//同步
	q.GET("", func(c *gin.Context) {
		time.Sleep(3*time.Second)
		log.Println("同步执行：" + c.Request.URL.Path)
	})


	q.Run(":8085")
}
